package com.leedsoft.spittr.web;

import com.google.common.collect.Lists;
import com.leedsoft.generated.Users;
import com.leedsoft.spittr.CounterSessionBean;
import com.leedsoft.spittr.CounterSessionBeanImpl;
import com.leedsoft.spittr.ICounterSessionBean;
import com.leedsoft.spittr.aop.proxy_test.cglib.BookFacadeImpl2;
import com.leedsoft.spittr.aop.proxy_test.jdk.BookFacadeImpl;
import com.leedsoft.spittr.model.User;
import com.leedsoft.spittr.qualifers.Book1;
import com.leedsoft.spittr.qualifers.Book2;
import com.leedsoft.spittr.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by leeds on 2017/11/14.
 */
@Controller
@RequestMapping("data")
public class DataController {
    @Autowired
    private DataService dataService;

    @RequestMapping("testJndiDatasource")
    @ResponseBody
    public List<User> testJndiDatasource() {
        List<Users> usersList = dataService.getUsersList();
        List<User> res = Lists.transform(usersList, u1 -> {
           User u2 = new User();
           u2.setName(u1.getUsername());
           return u2;
        });
        return res;
    }

    @RequestMapping("testTx")
    @ResponseBody
    public String testTx() {
//        dataService.insertOne();
        dataService.noTx();
        return "success";
    }

    @Autowired(required = false)
    private BookFacadeImpl bookFacade;

    @RequestMapping("testConditionalBean")
    @ResponseBody
    public String testConditionalBean() {
        return bookFacade == null ? "BookFacade未装配" : "BookFacade已装配";
    }

    @Autowired
    @Book2
    private BookFacadeImpl2 bookFacade2;//IDEA 自动检测装配是否歧义

    @RequestMapping("testPrimaryBean")  //字符串，对重构不友好
    @ResponseBody
    public String testPrimaryBean() {
        return bookFacade2.getName();
    }

    @Autowired
    private ICounterSessionBean sessionCounter;

    @RequestMapping("testSessionBean")
    @ResponseBody
    public String testSessionBean() {
        sessionCounter.setCount(sessionCounter.getCount() + 1);
        return sessionCounter.getCount().toString();
    }
}
